#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import weibo_login
import user_info
import json
import traceback
import xlwt
import xlrd
import os, time,random
from xlutils.copy import copy


def get_fans(uid, since_id=0):
    """
    爬取粉丝列表
    :param uid: 用户id
    :param since_id: 分页参数
    :return:
    """
    request_url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_%s' % uid
    if since_id > 0:
        request_url = request_url + '&since_id=%d' % since_id
    s = weibo_login.s
    headers = {
        'Referer': request_url,
        'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Mobile Safari/537.36'}

    try:
        res = s.get(request_url, headers=headers)
        res.raise_for_status()
    except Exception:
        print("获取粉丝列表失败")
        return None
    # print(res.text)
    return res.text
    pass


def get_fans_info(uid):
    """
    爬取用户资料详情
    :param uid: 用户id
    :return:
    """
    info_url = 'https://m.weibo.cn/api/container/getIndex?containerid=230283%s_-_INFO' % uid
    headers = {
        'Referer': 'https://m.weibo.cn/p/index?containerid=230283%s_-_INFO' % uid,
        'X-Requested-With': 'XMLHttpRequest',
        # 'X-XSRF-TOKEN': 'ea81d7',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Mobile Safari/537.36'}
    s = weibo_login.s
    try:
        res = s.get(info_url, headers=headers)
        res.raise_for_status()
        print(res.text)
        return res.text
    except Exception as e:
        print("获取用户详情失败")
        traceback.print_exc()
        return None


def covert_to_user(uid, since_id=0):
    list_user = []
    """
    解析json,封装为user对象
    :return:
    """
    fans_list = get_fans(uid, since_id)
    if fans_list is None:
        print('获取粉丝列表失败')
        return
    json_list = json.loads(fans_list)
    cards = json_list['data']['cards']
    cards_group = cards[len(cards) - 1]['card_group']
    for c in cards_group:
        user = user_info.User()
        # print(str(c['user']['id']) + '-----' + c['user']['screen_name'])
        user.user_id = c['user']['id']
        user.name = c['user']['screen_name']
        list_user.append(user)

    print("爬取[%d]个用户" % len(list_user))
    # print(list_user)
    list_user_detail = []
    for u in list_user:
        _info = get_fans_info(u.user_id)
        if _info is None:
            continue
        list_user_detail.append(u)
        json_info = json.loads(_info)
        info_list = json_info['data']['cards']
        cards_group = info_list[1]['card_group']
        for il in cards_group:
            if 'item_name' in il:
                if il['item_name'] == '性别':
                    u.sex = il['item_content']
                elif il['item_name'] == '所在地':
                    u.city = il['item_content']
    write_to_file(list_user_detail)


def write_to_file(user=None):
    nrows = 0
    list_user_a = user_to_array(user)
    if list_user_a is not None:
        if os.path.exists('user.xls') is True:
            data = xlrd.open_workbook('user.xls')
            sheet = data.sheet_by_index(0)
            nrows = sheet.nrows
            wt = copy(wb=data)
            sheet_wt = wt.get_sheet(0)
            for j in range(1, len(list_user_a) + 1):
                for k in range(0, len(list_user_a[j - 1])):
                    sheet_wt.write(nrows + j - 1, k, list_user_a[j - 1][k])
            wt.save('user.xls')
            print('追加excel文件成功')
        else:
            value_title = ["id", "姓名", "性别", "年龄", "城市"]
            workbook = xlwt.Workbook()
            sheet = workbook.add_sheet('user_info', cell_overwrite_ok=True)
            for i in range(0, len(value_title)):
                if i == 0:
                    for j in range(0, len(value_title)):
                        sheet.write(i, j, value_title[j])
            for j in range(1, len(list_user_a) + 1):
                for k in range(0, len(list_user_a[j - 1])):
                    sheet.write(j, k, list_user_a[j - 1][k])
            workbook.save('user.xls')
            print('写入excel文件成功')


def user_to_array(user=None):
    if user is None or len(user) == 0:
        return None
    list_user_array = []
    for u in user:
        user_array = [str(u.user_id), u.name, u.sex, '0', u.city]
        list_user_array.append(user_array)
    return list_user_array


def wei_bo_spilder(uid=None, since_id=0):
    for i in range(0, since_id):
        print("爬取第{}页", i)
        time.sleep(random.randint(1, 3))
        covert_to_user(uid, i)


if __name__ == '__main__':
    # get_fans("1776448504", 0)
    # get_fans('1776448504')
    wei_bo_spilder('1776448504', 100)
